package com.crane.common.framework.shiro;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;

public class ShiroRedisCacheManager implements CacheManager {

    public static final String DEFAULT_CACHE_KEY_PREFIX = "shiro:cache:";

    /*永不过期*/
    private int expire = 60 * 60 * 2;

    private String keyPrefix = DEFAULT_CACHE_KEY_PREFIX;

    private RedisTemplate<Object, Serializable> redisTemplate;

    @Override
    public <K, V> Cache<K, V> getCache(String name) throws CacheException {
        return new ShiroRedisCache<K, V>(expire, keyPrefix, redisTemplate);
    }

    public int getExpire() {
        return expire;
    }

    public void setExpire(int expire) {
        this.expire = expire;
    }

    public String getKeyPrefix() {
        return keyPrefix;
    }

    public void setKeyPrefix(String keyPrefix) {
        this.keyPrefix = DEFAULT_CACHE_KEY_PREFIX + keyPrefix + ":";
    }

    public RedisTemplate<Object, Serializable> getRedisTemplate() {
        return redisTemplate;
    }

    //	@Autowired
//	@Qualifier("shiroRedisCacheRedisTemplate")
    public void setRedisTemplate(RedisTemplate<Object, Serializable> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

}
